/*
 * @Author: boyyang
 * @Date: 2022-01-04 11:03:52
 * @LastEditTime: 2022-03-18 21:31:33
 * @LastEditors: boyyang
 * @Description: 用户信息
 * @FilePath: \boyyang\src\store\modules\user.ts
 */

import { defineStore } from 'pinia'
import { store } from '@/store'
import cloudbase from '@cloudbase/js-sdk'

interface IUserInfo {
    avatarUrl?: string
    nickName?: string
    email?: string
    city?: string
    province?: string
}

export interface IUserState {
    token: string
    roles: string[]
    userInfo?: cloudbase.auth.IUser | null
}

const useUserStore = defineStore({
    id: 'app-user',
    state: (): IUserState => ({
        token: 'user-token',
        roles: [],
        userInfo: {} as cloudbase.auth.IUser,
    }),

    getters: {
        getToken(): string {
            return this.token
        },
        getRoles(): string[] {
            return this.roles
        },
        getUserInfo(): any {
            return this.userInfo
        },
    },

    actions: {
        setToken(token: string) {
            this.token = token
        },

        async setRoles(roles: string[]) {
            this.roles = roles
        },

        setUserInfo(userInfo: cloudbase.auth.IUser | null) {
            this.userInfo = userInfo
        },
    },
})

const useUserStoreWithOut = () => {
    return useUserStore(store)
}

export { useUserStore, useUserStoreWithOut }
